package com.cecsmsserve.controller;

import com.cecsmsserve.entity.HealthStatisticConfig;
import com.cecsmsserve.service.HealthStatisticConfigService;
import com.cecsmsserve.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/health/config")
public class HealthStatisticConfigController {

    @Autowired
    private HealthStatisticConfigService healthStatisticConfigService;

    /**
     * 获取用户可见的健康指标配置
     */
    @GetMapping("/{userId}")
    public CommonResult<List<HealthStatisticConfig>> getVisibleConfigs(@PathVariable Integer userId) {
        List<HealthStatisticConfig> configs = healthStatisticConfigService.getVisibleConfigsByUserId(userId);
        return CommonResult.success(configs);
    }

    /**
     * 保存用户的健康指标配置
     */
    @PostMapping("/{userId}")
    public CommonResult<Boolean> saveUserConfigs(
            @PathVariable Integer userId, 
            @RequestBody List<Integer> typeIds) {
        List<HealthStatisticConfig> configs = typeIds.stream()
            .map(typeId -> {
                HealthStatisticConfig config = new HealthStatisticConfig();
                config.setTypeId(typeId);
                config.setIsVisible(true);
                return config;
            })
            .collect(Collectors.toList());
        boolean success = healthStatisticConfigService.saveUserConfig(userId, configs);
        return success ? CommonResult.success(true) : CommonResult.failed("保存配置失败");
    }

    /**
     * 更新配置的可见性
     */
    @PutMapping("/visibility")
    public CommonResult<Boolean> updateConfigVisibility(
            @RequestParam Integer id,
            @RequestParam Integer userId,
            @RequestParam Boolean isVisible) {
        boolean success = healthStatisticConfigService.updateConfigVisibility(id, userId, isVisible);
        return success ? CommonResult.success(true) : CommonResult.failed("更新可见性失败");
    }

    /**
     * 更新配置的显示顺序
     */
    @PutMapping("/order")
    public CommonResult<Boolean> updateConfigOrder(
            @RequestParam Integer id,
            @RequestParam Integer userId,
            @RequestParam Integer displayOrder) {
        boolean success = healthStatisticConfigService.updateConfigOrder(id, userId, displayOrder);
        return success ? CommonResult.success(true) : CommonResult.failed("更新顺序失败");
    }
} 